package doublepointer;

/**
 * 无重复字符的最长子串
 * 思路：滑动窗口，用hashset记录当前窗口内元素，当发现重复元素时，移动左指针，直到不重复为止
 */

import java.util.HashSet;

public class lengthOfLongestSubstring {
    public int lengthOfLongest(String s) {
        int left = 0,right = 0,max = 0;
        HashSet<Character> set = new HashSet<>();
        char[] chars = s.toCharArray();
        for (right = 0;right<s.length();++right){
            if(set.contains(chars[right])){
                while (set.contains(chars[right])){
                    set.remove(chars[left++]);
                }
            }
            set.add(chars[right]);
            max = Math.max(max,right-left+1);
        }
        return max;
    }

    public static void main(String[] args) {
        String s = "abcabcbb";
        lengthOfLongestSubstring lengthOfLongestSubstring = new lengthOfLongestSubstring();
        int i = lengthOfLongestSubstring.lengthOfLongest(s);
        System.out.println(i);
    }
}
